<?php
    include_once("../inc/login_controll.inc.php");
	include_once("../class/config.class.php"); 
	$config = config::getConfig();
	include_once($config->getValue("home_path")."/inc/defines.inc.php");

	include_once(HOME_PATH."/class/form.class.php");
	include_once(HOME_PATH."/class/database.class.php");
	include_once(HOME_PATH."/class/user.class.php");
	$oForm = new form($config);
	$oDB = new database($config);
	$oForm->setDB($oDB);
	if(isset($_POST['save']) && trim($_POST['save'])=="form"){
		include(HOME_PATH."/templates/header.tpl.php");
		$oForm->editForm($_POST['name'], $_POST['f'], $_POST['desc']);
		echo '<a href="teacher.php" alt="zurück" title="zurück"><img src="../templates/img/back.png" alt="Zur&uuml;ck" title="Zur&uuml;ck" /></a> <a href="new_quest.php?form='.$_POST['f'].'" alt="new Question" title="new Question"><img src="../templates/img/new_question.png" alt="Neue Frage hinzufügen" title="Neue Frage hinzufügen" /></a>';
		include(HOME_PATH."/templates/footer.tpl.php");
	}

	if(isset($_POST['save']) && trim($_POST['save'])=="question"){		 
		$oForm->alterQuestion($_POST['question'], $_POST['q']);
		$iFormId = $oForm->getFormByQuestionId($_POST['q']);
		header("location:edit.php?f=$iFormId");
	}
	
	if(isset($_GET['f']) && $_GET['f']!=""){
		$iFormId = (int)$_GET['f'];
		if($oForm->userIsOwnerOfForm($_SESSION['user_id'], $iFormId)){
			$aForm = $oForm->getFormDetails($iFormId);	
			$aQuestions = $oForm->getQuestionsByFormNorm($iFormId);			
			include(HOME_PATH."/templates/header.tpl.php");				
			include(HOME_PATH."/templates/admin/edit_form.tpl.php");
			
			if(count($aQuestions)>0){
				echo "<table>";			
				for($i=0; $i<count($aQuestions); $i++){						
					include(HOME_PATH."/templates/admin/edit_question_row.tpl.php");
				}
				echo "</table>";
			}
			include(HOME_PATH."/templates/footer.tpl.php");			
		}else{
			header('location:teacher.php');
		}
	}	
	
	if(isset($_GET['q']) && $_GET['q']!=""){
		$iQuestionId = (int)$_GET['q'];
		$aQuestion = $oForm->getQuestionById($iQuestionId);
		include(HOME_PATH."/templates/header.tpl.php");		
		include(HOME_PATH."/templates/admin/edit_question.tpl.php");
		include(HOME_PATH."/templates/footer.tpl.php");		
	}
?>